package com.dm0858.bianmin.ui.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dm0858.bianmin.R;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

public class QueryActivity extends AppCompatActivity {


    private Button OKBtn;
    private EditText phoneEdit;
    private TextView queryTextView;
    private String URL =  "http://bmshfw.net/phpapi/info.php?func=checkPhone";
    private boolean btnType = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        OKBtn = findViewById(R.id.OKBtn);
        phoneEdit = findViewById(R.id.phoneEdit);

        phoneEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
                btnType = false;
                OKBtn.setText("查询");
            }


            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        queryTextView = findViewById(R.id.queryTextView);


        OKBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard();
                if (btnType) {
                    //前往发布信息
                    Intent intent = new Intent(QueryActivity.this, CategoryActivity.class);
                    startActivity(intent);
                    return;
                }
                if (phoneEdit.getText().length() != 11){
                    Toast.makeText(QueryActivity.this, "手机号码格式有误",
                            Toast.LENGTH_SHORT).show();


                    return;
                }
                phoneQuery();
            }
        });

    }


    private void hideKeyboard() {

        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm.isActive() && this.getCurrentFocus() != null) {

            if (this.getCurrentFocus().getWindowToken() != null) {

                imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            }

        }

    }



    private void phoneQuery() {
        //

        AjaxParams params = new AjaxParams();
        params.put("phone", phoneEdit.getText().toString());


        FinalHttp fh = new FinalHttp();
        fh.post("http://bmshfw.net/phpapi/info.php?func=submitMsg", params, new AjaxCallBack() {

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                //status

                try {

                    JSONObject json = new JSONObject(o.toString());
                    int code = 0;
                    code = json.getInt("status");
                    hideKeyboard();
                    if (code == 1) {
                        btnType = true;
                        OKBtn.setText("去发布信息");
                        Toast.makeText(QueryActivity.this, "审核通过",
                                Toast.LENGTH_SHORT).show();
                    } else if (code == 0) {
                        btnType = false;
                        Toast.makeText(QueryActivity.this, "审核未通过",
                                Toast.LENGTH_SHORT).show();
                        OKBtn.setText("查询");
                    } else {
                        btnType = false;
                        Toast.makeText(QueryActivity.this, "审核未完成",
                                Toast.LENGTH_SHORT).show();
                        OKBtn.setText("查询");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(QueryActivity.this, "查询失败",
                        Toast.LENGTH_SHORT).show();

            }
        });

    }

}
